



//
//  BAFootNode.m
//  BabyAlbum
//
//  Created by master on 17/2/27.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BAFootNode.h"

#import "BAResource.h"
@interface BAFootNode () <ASEditableTextNodeDelegate>

@property (nonatomic, strong) ASEditableTextNode *titleNode;
@property (nonatomic, strong) ASTextNode *dateNode;
@property (nonatomic, strong) ASTextNode *weekNode;
@property (nonatomic, strong) ASDisplayNode *lineNode;
@property (nonatomic, strong) ASEditableTextNode *detailNode;
@property (nonatomic, strong) BAAlbumModel *model;
//@property (nonatomic, assign) BOOL isEditing;
@property (nonatomic, copy) void (^selectBlock)(void) ;
@property (nonatomic, copy) void (^titleChangeBlock)(NSString *text) ;
@property (nonatomic, copy) void (^detailTextChangeblock)(NSString *text) ;

@property (nonatomic, copy) void (^datePickerBlock)(void) ;

@end

@implementation BAFootNode

- (instancetype)initWithData:(BAAlbumModel *)model titleChangeBlock:(void (^)(NSString *text))titleChangeBlock detailChangeBlock:(void (^)(NSString *text))detailTextChangeblock datePickerBlock:(void (^)(void))datePickerBlock {
    if(self = [super init]) {
        self.titleChangeBlock = titleChangeBlock;
        self.detailTextChangeblock = detailTextChangeblock;
        self.datePickerBlock = datePickerBlock;
        self.model = model;
        
        _titleNode = [[ASEditableTextNode alloc] init];
        NSAttributedString *string = [NSAttributedString attributedStringWithString:model.title fontSize:24 color:[UIColor blackColor]];
        _titleNode.placeholderEnabled = YES;
        self.titleNode.attributedPlaceholderText = [NSAttributedString attributedStringWithString:@"点击输入标题" fontSize:15 color:RGBWhite(75)];
        _titleNode.attributedText = string;
        _titleNode.delegate = self;
        [self addSubnode:_titleNode];
        
        _dateNode = [[ASTextNode alloc] init];
        string =[NSAttributedString attributedStringWithString:[[model customDateShow] length]?[model customDateShow]:[[NSDate date] showDate] fontSize:13 color:RGB_255(188,197,195)];
        _dateNode.attributedText = string;
        [_dateNode addTarget:self action:@selector(dateSelect) forControlEvents:ASControlNodeEventTouchUpInside];
        [self addSubnode:_dateNode];
        

        _weekNode = [[ASTextNode alloc] init];
        string = [NSAttributedString attributedStringWithString:[[model weekDayShow] length]?[model weekDayShow]:[[NSDate date] weekDayShow] fontSize:13 color:RGB_255(188,197,195)];
        _weekNode.attributedText = string;
        [self addSubnode:_weekNode];
        
        _lineNode = [[ASDisplayNode alloc] init];
        _lineNode.backgroundColor = RGBWhite(140);
        [self addSubnode:_lineNode];
        
        _detailNode = [[ASEditableTextNode alloc] init];
        string = [NSAttributedString attributedStringWithString:model.desc fontSize:11 color:RGBWhite(140)];
        _detailNode.placeholderEnabled = YES;
        _detailNode.attributedPlaceholderText = [NSAttributedString attributedStringWithString:@"用文字记录难忘瞬间" fontSize:11 color:RGBWhite(180)];
        _detailNode.attributedText = string;
        _detailNode.delegate = self;

        [self setUpForDismissKeyboard];
        [self addSubnode:_detailNode];
    }
    return self;
}

- (void)setDate:(NSDate *)date {
    NSAttributedString *string =[NSAttributedString attributedStringWithString:[date showDate] fontSize:13 color:RGB_255(188,197,195)];
    _dateNode.attributedText = string;
    string = [NSAttributedString attributedStringWithString:[date weekDayShow] fontSize:13 color:RGB_255(188,197,195)];
    _weekNode.attributedText = string;
    
}

- (BOOL)editableTextNode:(ASEditableTextNode *)editableTextNode shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    return YES;
}

- (void)editableTextNodeDidUpdateText:(ASEditableTextNode *)editableTextNode {
    if(editableTextNode==_titleNode && self.titleChangeBlock) {
        self.titleChangeBlock(editableTextNode.textView.text);
    } else if(editableTextNode==_detailNode && self.detailTextChangeblock) {
        self.detailTextChangeblock(editableTextNode.textView.text);
    }
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    return NO;
}

- (void)dateSelect {
    if(self.datePickerBlock) {
        self.datePickerBlock();
    }
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
    
    _dateNode.style.preferredSize = CGSizeMake(constrainedSize.max.width - 60, 20);
    
    _titleNode.style.flexShrink  = NO;
    _detailNode.style.flexShrink = YES;
    _dateNode.style.flexShrink = NO;
    
    if(!self.model.desc.length) {
        _detailNode.style.preferredSize = CGSizeMake(constrainedSize.max.width - 10, 200);
    }
    _lineNode.style.preferredSize = CGSizeMake(constrainedSize.max.width - 20, 1);
    
    
    ASRelativeLayoutSpec *dateSpec = [ASRelativeLayoutSpec relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionStart verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOption:ASRelativeLayoutSpecSizingOptionMinimumHeight child:_dateNode];
    ASRelativeLayoutSpec *weekSpec = [ASRelativeLayoutSpec relativePositionLayoutSpecWithHorizontalPosition:ASRelativeLayoutSpecPositionEnd verticalPosition:ASRelativeLayoutSpecPositionCenter sizingOption:ASRelativeLayoutSpecSizingOptionMinimumHeight child:_weekNode];
    
    ASStackLayoutSpec *dataHor = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionHorizontal spacing:0 justifyContent:ASStackLayoutJustifyContentEnd alignItems:ASStackLayoutAlignItemsCenter children:@[dateSpec,weekSpec]];
    
    ASStackLayoutSpec *ver1 = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:5 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsCenter children:@[dataHor,_lineNode]];

    ASStackLayoutSpec *ver2 = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:10 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStart children:@[_titleNode,ver1,_detailNode]];
    
    
    
    ASInsetLayoutSpec *insetLayout2 = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(5, 5, 5, 5) child:ver2];
    
    return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) child:insetLayout2];
}

+ (CGFloat)calculteHeightWithData:(BAAlbumModel *)albumModel {
//    CGFloat descHeight = [albumModel.desc uxy_calculateSize:CGSizeMake(kScreenWidth - 30, CGFLOAT_MAX) font:[UIFont systemFontOfSize:11]].height;
    CGFloat descHeight = [UILabel getHeightByWidth:kScreenWidth - 30 title:albumModel.desc font:[UIFont systemFontOfSize:11]];

    if(descHeight < 10) {
        descHeight = 200;
    }
    CGFloat height = 5 + 20 + 5 + 13.5 + 5 + 1+ 5 + descHeight + 20;
    return height;
}

- (NSString *)detailText {
    return _detailNode.textView.text;
}

#pragma show and hide the keyboard
- (void)setUpForDismissKeyboard {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [KEYWINDOW addGestureRecognizer:singleTapGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [KEYWINDOW removeGestureRecognizer:singleTapGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    //此method会将self.view里所有的subview的first responder都resign掉
    [self.view endEditing:YES];
}

@end
